package net.sf.xoperator.ui.txt;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.openbbs.blackboard.filter.KindOfFilter;

import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.store.RdfStoreInfo;
import net.sf.xoperator.ui.StringMessage;

public class RemoveDatastoreCommand extends Command {

	@Override
	public String getHelp() {
		
		return "removes a prviously defined data store from the system. usage del ds <name>";
	}

	@Override
	public String getKeyWord() {
		// TODO Auto-generated method stub
		return "del ds";
	}
	
	@Override
	public List<Object> performInternal(String call, NoPlanBlackBoardControl control, Interactor interactor) {
      if (call.split(" ").length != 3){
         return Arrays.asList(new Object[] {new StringMessage("PLease check the Systax of the del ds command: " + getHelp())});
      }
		String storename = call.split(" ")[2];
		Set rdfinfos = control.getConfigurationContext().blackboard().readAll(new KindOfFilter(RdfStoreInfo.class));
		RdfStoreInfo store = null;
      for (Object object : rdfinfos) {
         RdfStoreInfo someInfo = (RdfStoreInfo) object;
         if(someInfo.getName().equals(storename)){
            store = someInfo;
         }
      }
      
      
      
		if(store==null){
			return Arrays.asList(new Object[] {new StringMessage("No such  store defiend")});
	
		}else{
			store.setObsolete(true);
			control.propagateConfiguration(store);
			return Arrays.asList(new Object[] {new StringMessage("Store removed")});
		}
	}
	

	@Override
	public Pattern getValidationPattern() {

		return Pattern.compile(".*");
	}

}
